//
//  MHCircleManageViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/4/20.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHCircleManageViewModel.h"
#import "MHCommonArrowItemViewModel.h"
#import "MHCommonSwitchItemViewModel.h"
#import "MHSettingPublishPermissionViewModel.h"
#import "MHSettingCircleJoinTypeViewModel.h"
#import "MHWebViewModel.h"
@implementation MHCircleManageViewModel

- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        self.circleInfo = params[MHViewModelUtilKey];
        
    }
    return self;
}
- (void)initialize{
    [super initialize];
    self.title = @"圈子管理";
    __weak __typeof(self)weakSelf = self;
    BOOL currentUserIsOwner = [self.circleInfo currentUserIsOwner];
    MHCommonGroupViewModel *group0 = [MHCommonGroupViewModel groupViewModel];
    
    MHCommonGroupViewModel *group1 = [MHCommonGroupViewModel groupViewModel];
    group1.footer = @"开启后圈子无法被搜索，内容无法推荐到发现";
    CGFloat limitWidth = MH_SCREEN_WIDTH - 2 * 20;
    CGFloat footerHeight1 = [group1.footer mh_sizeWithFont:MHRegularFont_14 limitWidth:limitWidth].height + 5 *2;
    group1.footerHeight = footerHeight1;
    
    
    
    MHCommonGroupViewModel *group2 = [MHCommonGroupViewModel groupViewModel];
    MHCommonGroupViewModel *group3 = [MHCommonGroupViewModel groupViewModel];
      group3.footer = @"开启后，可被分享到微信、朋友圈、QQ、微博";
    CGFloat footerHeight3 = [group3.footer mh_sizeWithFont:MHRegularFont_14 limitWidth:limitWidth].height + 5 *2;
    group3.footerHeight = footerHeight3;
    
    
    
    MHCommonGroupViewModel *group4 = [MHCommonGroupViewModel groupViewModel];
    group4.footer = @"关闭后，普通成员无法查看圈子内成员列表";
    CGFloat footerHeight4 = [group4.footer mh_sizeWithFont:MHRegularFont_14 limitWidth:limitWidth].height + 5 *2;
    group4.footerHeight = footerHeight4;
    
    
    MHCommonGroupViewModel *group5 = [MHCommonGroupViewModel groupViewModel];

    MHCommonArrowItemViewModel *editCell = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"成员加入方式"];
    editCell.subtitle = self.circleInfo.payType == MHCirclPayTypeFree  ? @"免费": @"付费";
    if (currentUserIsOwner) {
        editCell.destViewModelClass = MHSettingCircleJoinTypeViewModel.class;
        editCell.model = self.circleInfo;
    }
    
    //    editCell.rowHeight = 60;
    MHCommonSwitchItemViewModel *allowSearch = [MHCommonSwitchItemViewModel itemViewModelWithTitle:@"圈子加密"];
    allowSearch.off = self.circleInfo.isPublic; //是否加密  (YES = NO)
    allowSearch.offBlock = ^(BOOL off) {
        weakSelf.circleInfo.isPublic = off;
    };
    
    MHCommonArrowItemViewModel *allowPublish = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"设置发表权限"];
    allowPublish.destViewModelClass = MHSettingPublishPermissionViewModel.class;
    allowPublish.model = self.circleInfo;
    MHCommonSwitchItemViewModel *allowShare = [MHCommonSwitchItemViewModel itemViewModelWithTitle:@"允许分享主题"];
    allowShare.off = !self.circleInfo.isCanShare;
    allowShare.offBlock = ^(BOOL off) {
        weakSelf.circleInfo.isCanShare = !off;
    };
    
     MHCommonSwitchItemViewModel *openMember = [MHCommonSwitchItemViewModel itemViewModelWithTitle:@"开放成员列表"];
    openMember.off = !self.circleInfo.isOpenMember;
    openMember.offBlock = ^(BOOL off) {
        weakSelf.circleInfo.isOpenMember = !off;
    };
    
 
//    circelActive

    group0.itemViewModels = @[ editCell];
    group1.itemViewModels = @[ allowSearch];
    group2.itemViewModels = @[ allowPublish];
    group3.itemViewModels = @[ allowShare];
    group4.itemViewModels = @[ openMember];
    // group5.itemViewModels = @[ circelActive];
//    self.circleInfo.isOpenActivity = true;
    if(![self.circleInfo currentUserIsOwner])
    {
        if ([self.circleInfo currentUserIsOwner] && self.circleInfo.payType == MHCirclPayTypeCharge) {
            self.dataSource = @[group0,group1,group2,group3,group5];
        }else
        {
            self.dataSource = @[group0,group1,group2,group3];
        }
        
    }else
    {
        if ([self.circleInfo currentUserIsOwner] &&  self.circleInfo.payType == MHCirclPayTypeCharge) {
             self.dataSource = @[group0,group1,group2,group3,group4,group5];
        }else
        {
             self.dataSource = @[group0,group1,group2,group3,group4];
        }
    }

    
    
    @weakify(self);
    self.updateInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
     @strongify(self);
        return [self.services.client updateCircleSettingWithInfo:self.circleInfo];
    }];
}

@end
